util.js ➔ isCallableCheck   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
dl 0
loc 8
rs 9.4285
nop 1
1
/**
2
 * formatQuery - format a uri query string into a query object;
3
 * @param {String} queries - uri query string
4
 * @return {Object} - query object
5
 */
6
export const formatQuery = (queries) => {
7
  return queries.length ?
8
    queries.substr(1).split('&').reduce((queryObj, query) => {
9
      queryObj[decodeURI(query.split('=')[0])] = decodeURI(query.split('=')[1]);
10
      return queryObj;
11
    }, {}) : {};
12
};
13
14
15
/**
16
 * pickQuery - construct query object from an absolute url
17
 * @param {String} path - absolute url
18
 * @return {Object} - query object
19
 */
20
export const pickQuery = (path) => {
21
  const splitRoute = path.split('?');
22
  return splitRoute.length > 1 ? formatQuery(`?${splitRoute[1]}`) : {};
23
};
24
25
26
/**
27
 * getParams - contruct the real url and param object from active routes
28
 * @param {String} activeRoute - current route to generate param for
29
 * @param {String} path - current url
30
 * @return {Object} - both the true url and the param object
31
 */
32
export const getParams = (activeRoute, path) => {
33
  // split routes into sections
34
  const splitRoute = activeRoute.split('/');
35
  const splitPath = path.split('/');
36
37
  // Loop through each section in the active route
38
  const params = splitRoute.reduce((param, route, index) => {
39
    // check if section is the param
40
    if (route.match(':')) {
41
      // set the value of the param
42
      param[route.substr(1)] = splitPath[index];
43
    }
44
    return param;
45
  }, {});
46
47
  // Loop through each section in the real route to construct a url
48
  const trueRoute = splitRoute
49
    .map((routeFrag) => params[routeFrag.substr(1)] || routeFrag)
50
    .join('/');
51
52
  return { trueRoute, params };
53
}
54
/**
55
 * isCallableCheck - checks if a function is a callable or constructable
56
 * and a boolean in the either cases
57
 * @export
58
 * @param {function} obj 
59
 * @returns {boolean}  - returns true or false
60
 */
61
export function isCallableCheck(obj) {
62
  try {
63
    obj();
64
  } catch (err) {
65
    return false;
66
  }
67
  return true;
68
}
69